package com.shelpe.services.lexicon.simba;

import java.util.List;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.shelpe.services.lexicon.simba.WordClient.WordClientFallback;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import rx.Observable;

@FeignClient(name="${feign.simba.name}", url="${feign.simba.url}", fallback = WordClientFallback.class)
public interface WordClient {
	@RequestMapping(path = "/simba/taobao.simba.insight.wordsdata.get", method = RequestMethod.GET)
	Observable<GlobalWordRespone> findGlobalData(@RequestParam("bidword_list") String[] words, @RequestParam("start_date")String startDate, 
			@RequestParam("end_date")String endDate, @RequestHeader("tenantID") long tenantID);
	
	@Getter
	@Setter
	public static class GlobalWordRespone extends SimbaProxyBaseDto{		
		@JsonProperty("simba_insight_wordsdata_get_response")
		private GlobalWordRes res;
	}
	
	@Getter
	@Setter
	public static class GlobalWordRes{		
		@JsonProperty("word_data_list")
		private GlobalWordDataList dataList;
	}
	
	@Data
	public class GlobalWordDataList {
		@JsonProperty("insight_word_data_d_t_o")
		private List<InsightWordData> words;
	}
	
	
	@RequestMapping(path = "/simba/taobao.simba.insight.catsworddata.get", method = RequestMethod.GET)
	Observable<CategoryWordResponse> findCategoryData(@RequestParam("bidword_list") String[] words, @RequestParam("start_date")String startDate, 
			@RequestParam("end_date")String endDate, @RequestParam("cat_id")String categoryID , @RequestHeader("tenantID") long tenantID);
	
	@Getter
	@Setter
	public static class CategoryWordResponse extends SimbaProxyBaseDto{		
		@JsonProperty("simba_insight_catsworddata_get_response")
		private CategoryWordRes res;
	}
	
	@Getter
	@Setter
	public static class CategoryWordRes{		
		@JsonProperty("catword_data_list")
		private CategoryWordDataList dataList;
	}
	
	@Data
	public class CategoryWordDataList {
		@JsonProperty("insight_word_data_under_cat_d_t_o")
		private List<InsightWordData> words;
	}
	
	
	@RequestMapping(path = "/simba/taobao.simba.insight.wordssubdata.get", method = RequestMethod.GET)
	Observable<NetworkWordResponse> findNetworkData(@RequestParam("bidword_list") String[] words, @RequestParam("start_date")String startDate, 
			@RequestParam("end_date")String endDate, @RequestHeader("tenantID") long tenantID);
	
	@Getter
	@Setter
	public static class NetworkWordResponse extends SimbaProxyBaseDto{
		@JsonProperty("simba_insight_wordssubdata_get_response")
		private NetworkWordRes res;
	}
	
	@Getter
	@Setter
	public static class NetworkWordRes{
		@JsonProperty("word_subdata_list")
		private NetworkWordDataList dataList;
	}
	
	@Data
	public class NetworkWordDataList {
		@JsonProperty("insight_word_sub_data_d_t_o")
		private List<InsightWordData> words;
	}
	
	@Service
	@Slf4j
	public static class WordClientFallback implements WordClient{

		@Override
		public Observable<GlobalWordRespone> findGlobalData(String[] words, String startDate, String endDate,
				long tenantID) {
			log.error("fallback findGlobalData: {}", words != null?words:"null");
			return Observable.just(new GlobalWordRespone());
		}

		@Override
		public Observable<CategoryWordResponse> findCategoryData(String[] words, String startDate, String endDate,
				String categoryID, long tenantID) {
			log.error("fallback findCategoryData: {}", words != null?words:"null");
			return Observable.just(new CategoryWordResponse());
		}

		@Override
		public Observable<NetworkWordResponse> findNetworkData(String[] words, String startDate, String endDate,
				long tenantID) {
			log.error("fallback findNetworkData: {}", words != null?words:"null");
			return Observable.just(new NetworkWordResponse());
		}
		
	}
}
